package com.java.rabbitmq.direct;

import com.java.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author Woo_home
 * @create 2020/5/27 11:02
 */

public class TestConsumer1 {

    private static final String QUEUE_NAME = "direct_queue";

    public static void main(String[] args) throws Exception {

        // 创建连接
        Connection connection = ConnectionUtils.getConnection();

        // 创建通道
        Channel channel = connection.createChannel();

        // 声明要关注的队列
        channel.queueDeclare(QUEUE_NAME, false, false, true, null);

        System.out.println("[1]队列正在监听消息：");

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println("[1] 接收到消息：" + message);
            }
        };

        // 自动回复队列应答，RabbitMQ 中的消息确认机制
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}
